package org.soretro.portfolio.view
{
	import com.google.analytics.AnalyticsTracker;
	import com.google.analytics.GATracker;
	import com.greensock.TweenLite;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.events.StateChangeEvent;
	import mx.managers.SystemManager;
	import mx.states.State;
	
	import org.robotlegs.mvcs.Mediator;
	import org.soretro.portfolio.controller.events.LoadingBarsEvent;
	import org.soretro.portfolio.controller.events.MainNavigationEvent;
	import org.soretro.portfolio.helpers.GoogleAnalytics;
	import org.soretro.portfolio.helpers.Helper;
	import org.soretro.portfolio.helpers.swfAddress.SWFAddressEvent;
	import org.soretro.portfolio.model.MainNavigationModel;
	import org.soretro.portfolio.view.videoPlayer.events.VideoPlayerEvent;
	
	public class MainViewMediator extends Mediator
	{
		[Inject]
		public var view:MainView;
		
		[Inject]
		public var model:MainNavigationModel;
		
		private var timer:Timer;
		public var tracker:AnalyticsTracker;
		
		override public function onRegister():void
		{
//			googleTracker();
			
			GoogleAnalytics.getInstance().send( "Flash loaded ok." );
			
			timer = new Timer( 1500, 1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, stopLoadingBars );
			
			view.contactsPopup.closeBttn.addEventListener( MouseEvent.CLICK, onCloseClicked );
			view.contactsPopup.modalPanel.addEventListener( MouseEvent.CLICK, onCloseClicked );
			
			eventMap.mapListener( eventDispatcher, MainNavigationEvent.CLICKED, mainNaviClicked );
			eventMap.mapListener( view, StateChangeEvent.CURRENT_STATE_CHANGE, onStageChange );
			eventMap.mapListener( eventDispatcher, "contactsClicked", onContactsClicked );
			
		}
		
		private function onCloseClicked( event:MouseEvent ):void
		{
			GoogleAnalytics.getInstance().send( "Contact/closed" );
//			tracker.trackPageview( "Contact/closed" );
			TweenLite.to( view.contactsPopup, 1, { alpha:0, onComplete:hideContactsPopup } );
		}
		
		private function hideContactsPopup():void
		{
			view.contactsPopup.visible = false;
		}
		
		private function onContactsClicked( event:Event ):void
		{
			GoogleAnalytics.getInstance().send( "Contact/open" );
			view.contactsPopup.alpha = 0;
			view.contactsPopup.visible = true;
			TweenLite.to( view.contactsPopup, 1, { alpha:1 } );
		}
		
		private function onStageChange( event:StateChangeEvent ):void
		{
			dispatch( new MainNavigationEvent( 0, MainNavigationEvent.STATE_CHANGE ) );
		}
		
		public function handleSWFAddress(event:SWFAddressEvent):void {
			Helper.handleSWFAddress(view, event);
		}
		
		private function mainNaviClicked( event:MainNavigationEvent ):void
		{
			dispatch( new LoadingBarsEvent( LoadingBarsEvent.LOADING, "LOADING..." ) );
			
			
//			timer.delay = Math.random() * 500 + 1000;
			timer.start();
			
			TweenLite.to( view.viewStack, 1.5, { alpha:0, onComplete:changePage, onCompleteParams:[ State( view.states[ event.id ] ).name as String ] } );
		}
		
		private function changePage( param1:String ):void
		{
//			model.changePage( event.id );
			view.setCurrentState( param1 );
			Helper.setSWFAddress( view );
			TweenLite.to( view.viewStack, 1.5, { alpha:1 } );
		}
		
		protected function stopLoadingBars(event:TimerEvent=null):void
		{
			dispatch( new LoadingBarsEvent( LoadingBarsEvent.LOAD_END, "" ) );
		}
		
		private function googleTracker():void
		{
			tracker = new GATracker( SystemManager.getSWFRoot(this).stage, "UA-18699334-1", "AS3", false );
		}
	}
}